﻿using ConfExpert.Forms.Base;
using ConfExpert.Forms.Depo;
using ConfExpert.Forms.Depo.Aksesuar;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.Aksesuar
{
    public partial class AtilAksesuar : XForm
    {
        public AtilAksesuar()
        {
            InitializeComponent();
        }

        private void AtilAksesuar_Load(object sender, EventArgs e)
        {
            gvAtil.Init("GetAtilAksesuar",null,null);
            gvAtil.Columns["AksesuarId"].ColumnEdit = new RepositoryItemTextEdit();
            gvAtil.Columns["AksesuarId"].OptionsColumn.AllowEdit = false;
            gvAtil.Columns["AksesuarId"].AppearanceCell.BackColor = System.Drawing.Color.LightYellow;

            DataTable dtDonem = Session.DataManager.ExecuteDataTableByQuery("Select AksesuarDonemId,Name,Yil,Donem,BaslangicTarihi,BitisTarihi from AksesuarDonem where Tip = 'Atıl'");
            DevExpress.XtraEditors.Repository.RepositoryItemGridLookUpEdit riMyBrands = GetCustomGLE(dtDonem, "Name", "AksesuarDonemId");
            gvAtil.Columns["AksesuarDonemId"].ColumnEdit = riMyBrands;

            gvAtil.SetCaptions("AksesuarDonemId:DonemId");

            gvAtil.Columns["NotSayisi"].ColumnEdit = HyperLink();

            gvAtil.FormatAsNumber(5, "BirimFiyat");
            gvAtil.FormatAsNumber(2, "Tutar");

            gvAtil.AddSummary("RaftakiMiktar");
            gvAtil.MakeReadOnly("FaturaNo", "AksesuarDonemId");

            if (!ytk(208))
                bbiKaydet.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            gvAtil.RowCellClick += gridView1_RowCellClick;
            gvAtil.DoubleClick += gvAtil_DoubleClick;
            bbiKaydet.ItemClick += bbiKaydet_ItemClick;

            gvAtil.BestFitColumns();
        }

        void bbiKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAtil.EndEditing();
            string xml = gvAtil.SerializeChanges();
            ExecuteNonQuery("UpdAtilAksesuarDonem", xml);
        }

        void gvAtil_DoubleClick(object sender, EventArgs e)
        {
            int handle = gvAtil.FocusedRowHandle;

            if (handle > -1)
            {
                int AksesuarId = intParse(gvAtil.GetRowCellValue(handle, "AksesuarId"));
                using (AksesuarHareketGC ah = new AksesuarHareketGC(AksesuarId, null, null))
                    ah.ShowDialog();
            }
        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAtil.RefreshDataSource("GetAtilAksesuar",deBaslangicTarihi.EditValue,deBitisTarihi.EditValue);
            gvAtil.BestFitColumns();
        }


        void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Column.FieldName == "NotSayisi")
                if (gvAtil.GetFocusedDataRow()["NotSayisi"].ToStr() != string.Empty)
                {
                    int aksId = gvAtil.GetFocusedDataRow()["AksesuarId"].ToInt(-1);
                    using (AksPasifNotDetay notDetay = new AksPasifNotDetay(aksId))
                        notDetay.ShowDialog();
                }
        }

        public static RepositoryItemHyperLinkEdit HyperLink()
        {
            RepositoryItemHyperLinkEdit riNoteCount = new RepositoryItemHyperLinkEdit();
            riNoteCount.SingleClick = true;
            return riNoteCount;
        }
    }
}
